<script setup>
import { ref, defineExpose, defineEmits } from 'vue'
import { artAddService, artgetDetailService, artupdateService } from '@/api/atricle'
import chselect from '@/components/channelSelect.vue'
import { QuillEditor } from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import { baseURL } from '@/utils/requset'
import axios from 'axios'

const drawer = ref(false)
const form = ref()
const edit = ref()
const defaultForm = ref({
  title: '',
  cate_id: '',
  cover_img: '',
  content: '',
  state: ''
})
const imgUrl = ref('')
const onSelectFile = (uploadFild) => {
  imgUrl.value = URL.createObjectURL(uploadFild.raw)
  formModel.value.cover_img = uploadFild.raw
}
const formModel = ref({
  ...defaultForm.value
})
const open = async (row) => {
  drawer.value = true
  //数据回显
  if (row.id) {
    const res = await artgetDetailService(row.id)
    formModel.value = res.data.data
    imgUrl.value = baseURL + formModel.value.cover_img
    formModel.value.cover_img = await imgurltodileobj(imgUrl.value, formModel.value.cover_img)
  } else {
    //添加数据
    formModel.value = {
      ...defaultForm.value
    }
    imgUrl.value = baseURL
    edit.value.setHTML('')
  }
}
const emit = defineEmits(['success'])
//发布按钮
const onPublish = async (value) => {
  formModel.value.state = value
  //接口参数是formdata类型，要转换一下
  const fd = new FormData()
  for (let key in formModel.value) {
    fd.append(key, formModel.value[key])
  }
  //发布请求
  if (formModel.value.id) {
    //编辑操作
    await artupdateService(fd)
    ElMessage.success('修改成功')
    drawer.value = false
    emit('success', 'updata')
  } else {
    //添加操作
    await artAddService(fd)
    ElMessage.success('添加成功')
    drawer.value = false
    emit('success', 'add')
  }

}
async function imgurltodileobj(url, name) {
  try {
    const res = await axios.get(url, { responseType: 'arraybuffer' })
    const blob = new Blob([res.data], {
      type: res.headers['content-type']
    })
    const file = new File([blob], name, {
      type: res.headers['content-type']
    })
    return file
  } catch (error) {
    console.log(error);
  }
}
defineExpose({
  open
})
</script>
<template>
  <el-drawer v-model="drawer" :title="formModel.id ? '文章编辑' : '添加文章'">
    <el-form :model="formModel" ref="form">
      <el-form-item label="文章标题">
        <el-input v-model="formModel.title"></el-input>
      </el-form-item>
      <el-form-item label="文章分类">
        <chselect v-model="formModel.cate_id" width="100%"></chselect>
      </el-form-item>
      <el-form-item label="文章封面">
        <el-upload class="avatar-uploader" :show-file-list="false" :auto-upload="false"
          :on-change="onSelectFile">
          <img v-if="imgUrl" :src="imgUrl" class="avatar" alt="">
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>

      </el-form-item>
      <el-form-item label="文章内容">
        <div class="editor">
          <QuillEditor theme="snow" ref="edit" content-type="html"
            v-model:content="formModel.content">
          </QuillEditor>
        </div>

      </el-form-item>
      <el-form-item>
        <el-button @click="onPublish('已发布')" type="primary">发布</el-button>
        <el-button @click="onPublish('草稿')" type="info">草稿</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      width: 178px;
      height: 178px;
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transtion-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-border-color);
    }

    .el-icon .avatar-uploader-icon {
      font-size: 28px;
      color: #8c8c8c;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-width: 200px;
    min-height: 100px;
  }
}
</style>